package com.show.web.sys_menu;

import com.show.dto.sys_menu.SysMenu;
import com.show.filter.LoginController;
import com.show.service.sys_menu.MenuService;
import com.show.util.HelperClazz;
import com.show.util.PageUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by 顾志杰 on 2018/2/1.
 */
@Controller
@RequestMapping(value = "/menu")
public class SysMenuController {
    @Autowired
   private MenuService menuService;
    @Autowired
    private LoginController loginController;
    @GetMapping("/header")
    @ApiOperation(value = "跳转RoleInsert", notes = "")
    public String header(Map<String, Object> map) throws Exception {
        return "header";
    }
    @GetMapping("toCgMenu")
    @ApiOperation(value = "跳转菜单主页", notes = "")
    public String toequipment(Map<String, Object> map, HttpServletRequest request) throws Exception {
        Map<String, Cookie> cookieMap = HelperClazz.ReadCookieMap(request);
        Cookie cookie = (Cookie) cookieMap.get("userName");
        String userName = cookie.getValue().replace("'","");
        List<SysMenu> list = loginController.getMenus(userName);
        String empName = loginController.getEmp(userName);
        map.put("list",list);
        map.put("empName",empName);
        return "menu_manage/menu_index";
    }
    @GetMapping("toCgMenuList")
    @ApiOperation(value = "跳转查询菜单页", notes = "")
    @ApiImplicitParams({@ApiImplicitParam(name = "menuName", paramType = "query", value = "菜单名称", dataType = "String")
            , @ApiImplicitParam(name = "page", paramType = "query", value = "页码",required = true, dataType = "Integer")
            , @ApiImplicitParam(name = "pageSize", paramType = "query", value = "页容", required = true, dataType = "Integer")
    })
    public String toCgmentList(Map<String, Object> map, SysMenu cgMenu, Integer page, Integer pageSize) throws Exception {
       if(StringUtils.isNotBlank(cgMenu.getMenuName())){
           map.put("menuName",cgMenu.getMenuName());
       }
        PageUtil<SysMenu> cgMenus = menuService.findAll(cgMenu, page, pageSize);
        map.put("datas", cgMenus);
        return "menu_manage/menu_list";
    }
    @GetMapping("toCgMenuUpdate/{id}")
    @ApiOperation(value = "跳转修改菜单页", notes = "")
    @ApiImplicitParam(name = "id", paramType = "path", value = "菜单id", dataType = "String")
    public String toCgMenuUpdate(Map<String, Object> map, @PathVariable("id") Integer id) {
        SysMenu cgMenu = menuService.findOne(id);
        map.put("cgMenu", cgMenu);
        map.put("menuTree",menuService.treeParent());
      if(cgMenu.getpMenuId()!=null){
          map.put("pMenu",menuService.findOne(cgMenu.getpMenuId()));
      }else{
          SysMenu n = new SysMenu();
          map.put("pMenu",n);
      }

        return "menu_manage/menu_update";
    }
    @ApiOperation(value="删除单个设备", notes="按设备ID删除设备信息")
    @PostMapping(value = "/delete/{menuid}")
    @ResponseBody
    public Integer delete(@PathVariable Integer menuid){
        Integer type = menuService.delete(menuid);
        return type;
    }
    @ApiOperation(value = "修改菜单", notes = "")
    @PostMapping(value = "update")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "menuId", paramType = "query", value = "菜单id", dataType = "String")
            , @ApiImplicitParam(name = "menuName", paramType = "query", value = "菜单名称", dataType = "String")
            , @ApiImplicitParam(name = "menuUrl", paramType = "query", value = "菜单地址", dataType = "String")
            , @ApiImplicitParam(name = "pMenuId", paramType = "query", value = "父级菜单id", dataType = "String")
            , @ApiImplicitParam(name = "level", paramType = "query", value = "层级", dataType = "String")
            , @ApiImplicitParam(name = "menuAttribution", paramType = "query", value = "菜单归属系统", dataType = "String")
            , @ApiImplicitParam(name = "menuCss", paramType = "query", value = "菜单样式", dataType = "String")
    })
    public String update(SysMenu cgMenu) {

        return menuService.update(cgMenu);
    }

}
